//
//  ESTFollowUpTextViewCell.m
//  ESTSCRMAPP
//
//  Created by 李贤立 on 2018/10/18.
//  Copyright © 2018年 Capfer. All rights reserved.
//

#import "ESTFollowUpTextViewCell.h"

@interface ESTFollowUpTextViewCell ()<UITextViewDelegate>
@property (weak, nonatomic) IBOutlet UILabel *leftTitleLabel;
@property (weak, nonatomic) IBOutlet UITextView *rightTextView;
@property (weak, nonatomic) IBOutlet UILabel *wordsLabel;
@property (weak, nonatomic) IBOutlet UITextField *rightPlaceholderTextField;
@end

@implementation ESTFollowUpTextViewCell

+ (instancetype)creatFollowUpTextViewCell:(UITableView *)tableView {
    ESTFollowUpTextViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ESTFollowUpTextViewCell"];
    if (cell == nil) {
        cell = [[NSBundle mainBundle] loadNibNamed:@"ESTFollowUpTextViewCell" owner:nil options:nil].lastObject;
    }
    return cell;
}
- (void)awakeFromNib {
    [super awakeFromNib];
    self.rightTextView.delegate = self;
    self.rightTextView.tag = TAG_SIXTH_ACTIVITY_FOLLOWUPTEXTVIEW;
    WEAKSELF
    [[NSNotificationCenter defaultCenter] addObserverForName:UITextViewTextDidChangeNotification object:nil queue:[NSOperationQueue currentQueue] usingBlock:^(NSNotification * _Nonnull note) {
        UITextView *textView = (UITextView *)note.object;
        if (textView.tag == TAG_SIXTH_ACTIVITY_FOLLOWUPTEXTVIEW) {
            NSInteger count = textView.text.length;
            if (count == 0) {
                weakSelf.rightPlaceholderTextField.placeholder = weakSelf.rightPlaceholder;
            } else {
                weakSelf.rightPlaceholderTextField.placeholder = @"";
            }
            weakSelf.wordsLabel.text = [NSString stringWithFormat:@"%ld/%ld", (long)count, (long)weakSelf.maxWords];
        }
    }];
}
#pragma mark - UITextViewDelegate
- (void)textViewDidBeginEditing:(UITextView *)textView {
    
}
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
    //获取当前cell相对屏幕的位置
    CGRect frame = [textView convertRect:textView.frame toView:nil];
    if (self.cellBecomeFirstResponderBlock) {
        self.cellBecomeFirstResponderBlock(frame);
    }
    return YES;
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    // 获取键盘中发送事件（回车事件）
    if ([text isEqualToString:@"\n"]) {
        if (self.didClickKeyboardReturn) {
            self.didClickKeyboardReturn();
        }
        [textView resignFirstResponder];
    }
    if (textView.text.length + text.length > self.maxWords) {
        return NO;
    }
    return YES;
}
#pragma mark - setter & getter
- (void)setMaxWords:(NSInteger)maxWords {
    _maxWords = maxWords;
    self.wordsLabel.text = [NSString stringWithFormat:@"0/%ld", (long)self.maxWords];
}
- (void)setLeftTitle:(NSString *)leftTitle {
    _leftTitle = leftTitle;
    self.leftTitleLabel.text = leftTitle;
}
- (void)setRightPlaceholder:(NSString *)rightPlaceholder {
    _rightPlaceholder = rightPlaceholder;
    self.rightPlaceholderTextField.placeholder = rightPlaceholder;
}
- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
